package Purl::filter;

sub timeFormat {

    my $secs = shift;
    if ( $secs < 60 ) {
        return "00:00";
    }    
    elsif ( $secs < 3600 ) {
        return sprintf( "00:%02d", int( $secs / 60 ) );
    }
    elsif ( $secs < 86400 ) {
        my $modulo = $secs % 3600;
        return
            sprintf( "%02d:%02d", int( $secs / 3600 ), int( $modulo / 60 ) );
    }
    else {
        my $modulo = $secs % 86400;
        return sprintf( "%dd %02d:%02d",
            int( $secs / 86400 ),
            int( $modulo / 3600 ),
            int( ( $modulo % 3600 ) / 60 ) );
    }
}

sub canonicalizeWord {
    my ( $me, $word ) = @_;
    $word =~ s/^\s+//g;
    $word =~ s/\s+$//g;
    if ( $word eq "me" ) {
        $word = "#" . $me;
    }
    elsif ( $word eq "here" ) {
        $word = "#" . $objects[$me]{"location"};
    }
    elsif ( substr( $word, 0, 1 ) eq "*" ) {
        my ($name);
        ( $name = substr( $word, 1 ) ) =~ tr/A-Z/a-z/;
        $word = "#" . $playerIds{$name} if ( exists( $playerIds{$name} ) );
    }
    return $word;
}

sub timeAndDateFormat {
    my ( $me, $when ) = @_;
    my ($adj)        = $when + $objects[$me]{"tz"} * 60;
    my (@timeFields) = gmtime($adj);
    my ( $month, $day, $year, $hour, $min, $sec );
    $month = (
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    )[ $timeFields[4] ];
    $day  = $timeFields[3];
    $year = $timeFields[5] + 1900;
    $hour = $timeFields[2];
    $min  = $timeFields[1];
    $sec  = $timeFields[0];
    my ($suffix);

    if ( $objects[$me]{"24hour"} ) {
        $suffix = "";
    }
    else {
        if ( ( $hour == 12 ) && ( $min == 0 ) ) {
            $suffix = " noon";
        }
        elsif ( ( $hour == 0 ) && ( $min == 0 ) ) {
            $hour   = 12;
            $suffix = " midnight";
        }
        elsif ( $hour > 12 ) {
            $hour -= 12;
            $suffix = " pm";
        }
        elsif ( $hour == 12 ) {
            $suffix = " pm";
        }
        elsif ( $hour == 0 ) {
            $hour   = 12;
            $suffix = " am";
        }
        else {
            $suffix = " am";
        }
    }
    if ( $objects[$me]{"24hour"} ) {
        $hour = sprintf( "%02d", $hour );
    }
    else {
        $hour = sprintf( "%2d", $hour );
    }
    return sprintf( "%s %02d %04d %02d:%02d:%02d$suffix",
        $month, $day, $year, $hour, $min, $sec );
}

sub substitute {
    my ( $me, $arg ) = @_;
    my ( $s, $p, $o, $n, $a, $r, $l, $dbref );
    $_     = $arg;
    $dbref = "#" . $me;
    $l     = "#" . $main::objects[$me]{"location"};
    return $arg if ( !/%/ );
    $s = Purl::tests::sexTest( $me, "she", "he", "it" );
    $arg =~ s/\%s/$s/ge;
    $s = ucfirst $s;
    $arg =~ s/\%S/$s/ge;
    $n = $main::objects[$me]{"name"};
    $arg =~ s/\%n/$n/ge;
    $n = ucfirst $n;
    $arg =~ s/\%N/$n/ge;
    $p = Purl::tests::sexTest( $me, "her", "his", "its" );
    $arg =~ s/\%p/$p/ge;
    $p = ucfirst $p;
    $arg =~ s/\%P/$p/ge;
    $a = Purl::tests::sexTest( $me, "hers", "his", "its" );
    $arg =~ s/\%a/$a/ge;
    $a = ucfirst $a;
    $arg =~ s/\%A/$a/ge;
    $o = Purl::tests::sexTest( $me, "her", "him", "its" );
    $arg =~ s/\%o/$o/ge;
    $o = ucfirst $o;
    $arg =~ s/\%O/$o/ge;
    $r = "\n";
    $t = "\t";
    $b = " ";
    $arg =~ s/\%b/$b/ge;
    $arg =~ s/\%B/$b/ge;
    $arg =~ s/\%t/$t/ge;
    $arg =~ s/\%T/$t/ge;
    $arg =~ s/\%r/$r/ge;
    $arg =~ s/\%R/$r/ge;
    $arg =~ s/\%\#/$dbref/ge;
    $arg =~ s/\%l/$l/gei;
    $arg =~ s/\%\%/\%/g;
    return $arg;
}

sub ansi {
    $arg = shift;

    #ANSI
    $arg =~ s/\%xh/\033[1m/g;
    $arg =~ s/\%xn/\033[0m/g;
    $arg =~ s/\%xu/\033[4m/g;
    $arg =~ s/\%xf/\033[5m/g;
    $arg =~ s/\%xi/\033[7m/g;
    $arg =~ s/\%xx/\033[30m/g;
    $arg =~ s/\%xr/\033[31m/g;
    $arg =~ s/\%xg/\033[32m/g;
    $arg =~ s/\%xy/\033[33m/g;
    $arg =~ s/\%xb/\033[34m/g;
    $arg =~ s/\%xm/\033[35m/g;
    $arg =~ s/\%xc/\033[36m/g;
    $arg =~ s/\%xw/\033[37m/g;
    $arg =~ s/\%xX/\033[40m/g;
    $arg =~ s/\%xR/\033[41m/g;
    $arg =~ s/\%xG/\033[42m/g;
    $arg =~ s/\%xY/\033[43m/g;
    $arg =~ s/\%xB/\033[44m/g;
    $arg =~ s/\%xM/\033[45m/g;
    $arg =~ s/\%xC/\033[46m/g;
    $arg =~ s/\%xW/\033[47m/g;
    return $arg . "\033[0m";
}
1;

